package com.sise.stuteaintersys.service.impl;

import com.sise.stuteaintersys.bean.entity.Admin;
import com.sise.stuteaintersys.bean.entity.Student;
import com.sise.stuteaintersys.bean.entity.Teacher;
import com.sise.stuteaintersys.bean.vo.LoginRequestVO;
import com.sise.stuteaintersys.bean.vo.LoginResponseVO;
import com.sise.stuteaintersys.bean.vo.RegisterRequestVO;
import com.sise.stuteaintersys.bean.vo.RegisterResponseVO;
import com.sise.stuteaintersys.mapper.AdminMapper;
import com.sise.stuteaintersys.mapper.StudentMapper;
import com.sise.stuteaintersys.mapper.TeacherMapper;
import com.sise.stuteaintersys.service.LoginService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName LoginServiceImpl
 * @Description 登陆时的业务验证
 * @Date 2018/12/29 22:29
 * @Version 1.0
 **/
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    protected Mapper dozerMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    AdminMapper adminMapper;

    /**
     * @Description 用来检测学生用户的密码是否正确
     * @Param com.sise.stuteaintersys.bean.protocol.LoginRequest
     * @Return com.sise.stuteaintersys.bean.protocol.LoginResponse
     * @Date 2018/12/28
     * @Time 15:26
     */
    @Override
    public LoginResponseVO checkStudent(LoginRequestVO loginRequest) {
        //获取密码
        LoginResponseVO loginResponseVO = new LoginResponseVO();
        try {
            Student student = studentMapper.selectPasswordByLoginName(loginRequest.getName());
            loginResponseVO = checkLogin(loginRequest, student.getPassword());
            if (loginResponseVO.getStatus()) {
                loginResponseVO.setData(student);
            }
            return loginResponseVO;
        }catch(Exception e){
            loginResponseVO.setStatus(false);
            loginResponseVO.setReason("输入登陆名");
            return loginResponseVO;
        }

    }


    /**
     * @Description 用来检测学生用户注册是否成功
     * @Param RegisterRequset
     * @Return RegisterResponse
     * @Date 2018/12/28
     * @Time 15:26
     */
    @Override
    public RegisterResponseVO checkRegisterStudent(RegisterRequestVO registerRequset, Date birthday) {

        Student student = dozerMapper.map(registerRequset, Student.class);
        RegisterResponseVO registerResponse = new RegisterResponseVO();
        student.setGmtModified(new Date());
        student.setGmtCreate(new Date());
        student.setBirthday(birthday);
        try {
            studentMapper.insert(student);
            registerResponse.setStatus(true);
        } catch (Exception e) {
            registerResponse.setStatus(false);
            registerResponse.setReason("用户名占用或者该生已注册");
        }
        return registerResponse;
    }

    @Override
    public LoginResponseVO checkTeacher(LoginRequestVO loginRequest) {
        LoginResponseVO loginResponseVO = new LoginResponseVO();
        try {
            Integer id = Integer.parseInt(loginRequest.getName());
            try {
                Teacher teacher = teacherMapper.selectByPrimaryKey(id);
                loginResponseVO = checkLogin(loginRequest, teacher.getPassword());
                if (loginResponseVO.getStatus()) {
                    loginResponseVO.setData(teacher);
                }
                return loginResponseVO;
            } catch (Exception e){
                loginResponseVO.setReason("用户不存在");
                loginResponseVO.setStatus(false);
                return loginResponseVO;
            }
        } catch (Exception e) {
            loginResponseVO.setReason("请输入工号");
            loginResponseVO.setStatus(false);
            return loginResponseVO;
        }

    }


    @Override
    public LoginResponseVO checkAdmin(LoginRequestVO loginRequest) {
        LoginResponseVO loginResponseVO = new LoginResponseVO();
        Integer id;
        try {
            id = Integer.parseInt(loginRequest.getName());
            try {
                Admin admin = adminMapper.selectByPrimaryKey(id);
                loginResponseVO = checkLogin(loginRequest, admin.getPassword());
                if (loginResponseVO.getStatus()) {
                    loginResponseVO.setData(admin);
                }
                return loginResponseVO;
            } catch (Exception e) {
                loginResponseVO.setReason("用户不存在");
                loginResponseVO.setStatus(false);
                return loginResponseVO;
            }
        } catch (Exception e) {
            loginResponseVO.setReason("请输入工号");
            loginResponseVO.setStatus(false);
            return loginResponseVO;
        }


    }


    public LoginResponseVO checkLogin(LoginRequestVO loginRequest, String password) {
        LoginResponseVO loginResponse = new LoginResponseVO();
        //应该是前端写的
        //验证是否信息未全
        if (loginRequest.getPassword() == null || loginRequest.getPassword().isEmpty()) {
            loginResponse.setReason("请输入密码");
            loginResponse.setStatus(false);
            return loginResponse;
        } else if (loginRequest == null) {
            loginResponse.setReason("请输入账号信息");
            loginResponse.setStatus(false);
            return loginResponse;
        }
        //获取密码
        //登陆验证密码的正确性
        if (loginRequest.getPassword().equals(password)) {
            loginResponse.setStatus(true);
            loginResponse.setReason("登陆成功");
        } else if (password != null) {
            loginResponse.setStatus(false);
            loginResponse.setReason("密码错误");
        }
        return loginResponse;
    }


}
